package com.sunsys.cxf.examples;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;

@Path("/categoryservice/")
@Produces("application/xml")
public class CategoryService {

	@GET
	@Path("/category/{id}")
	public Category getCategory( @PathParam("id") String id) {
//		System.out.println("get Category method - id is:"+id);
		/* can use the Dao or some other handler to pull the required details */
		/* in this example we have hard coded for simplicity  */
		Category category = new Category();
		category.setCategoryId("001");
		category.setCategoryName("IT");
		
		List<Book> booksList = new ArrayList<Book>();
		
		Book b1 = new Book();
		b1.setAuthor("Some one with out details about Gender");
		b1.setBookId("book-it-1");
		b1.setBookISBNnumber("123456789");
		b1.setBookName("book name 1");
		booksList.add(b1);

		category.setBooks(booksList);
		return category;
	}

	/**
	 * 
	 * If you prefer working with Jackson JSON providers then register either JacksonJsonProvider:
	 * 		<jaxrs:providers>
	 *   		 <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
	 * 		</jaxrs:providers>
	 * or JacksonJaxbJsonProvider (when working with JAXB beans):
	 * 
	 * 		<jaxrs:providers>
	 * 		   <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
	 * 		</jaxrs:providers>
	 * and add this Maven dependency:
	 * 		<dependency>
	 * 		  <groupId>org.codehaus.jackson</groupId>
	 * 		  <artifactId>jackson-jaxrs</artifactId>
	 * 		  <version>1.9.0</version>
	 * 		</dependency>
	 * 		<dependency>
	 * 			<groupId>org.codehaus.jackson</groupId>
	 * 			<artifactId>jackson-xc</artifactId>
	 * 			<version>1.9.13</version>
	 * 		</dependency>
	 * 
	 * 
	 *  Note:
	 *  -----
	 *  	if don't add <groupId>org.codehaus.jackson</groupId>  <artifactId>jackson-xc</artifactId>
	 *  	then we get the following exception in the server side
	 *  	java.lang.ClassNotFoundException: org.codehaus.jackson.xc.JaxbAnnotationIntrospector
	 * 

	 * 
	 * 
	 * 
	 * @param id
	 * @return
	 */
	@GET
	@Path("/category/{id}/json")
	@Produces("application/json")
	public Category getCategoryJson( @PathParam("id") String id) {
//		System.out.println("get Category method - id is:"+id);
		/* can use the Dao or some other handler to pull the required details */
		/* in this example we have hard coded for simplicity  */
		Category category = new Category();
		category.setCategoryId("001");
		category.setCategoryName("IT");
		
		List<Book> booksList = new ArrayList<Book>();
		
		Book b1 = new Book();
		b1.setAuthor("Some one with out details about Gender");
		b1.setBookId("book-it-1");
		b1.setBookISBNnumber("123456789");
		b1.setBookName("book name 1");
		booksList.add(b1);

		category.setBooks(booksList);
		return category;
	}
	
	@POST
	@Path("/category/{id}")
	public Category updateCategory(@PathParam("id") String id) {
		/* throw WebApplicationException with appropriate HTTP status */
		throw new WebApplicationException( 404);
		
		/**
		 * 	1)	Another approach to handle exception is by registering the ExceptionMapper	
		 * 		With this approach, once can
		 * 
		 *  2)	One can also register Custom CXF out fault interceptor which can handle all the exceptions 
		 *  	by writing directly to HttpServletsResponse stream or XMLStreamWriter
		 *   
		 */
	}
	
	@GET
	@Path("/category/{id}/{gender}")
	public Category getCategory( @PathParam("id") String id, @PathParam("gender") Gender gender) {
		/* can use the Dao or some other handler to pull the required details */
		/* in this example we have hard coded for simplicity  */
		Category category = new Category();
		category.setCategoryId("001");
		category.setCategoryName("IT");
		
		List<Book> booksList = new ArrayList<Book>();
		
		Book b1 = new Book();
		b1.setAuthor("Some one with Gender");
		b1.setBookId("book-it-1");
		b1.setBookISBNnumber("123456789");
		b1.setBookName("book name 1");
		booksList.add(b1);
		category.setBooks(booksList);
		
		return category;
	}
	
	@POST
	@GET
	@Path("/categoryWithContext/{id}")
	public Category getCategory(@Context UriInfo uriInfo, @Context HttpHeaders httpHeaders, @PathParam("id") String id) {
		/*
		 * 	A number of Context types can be inject as parameters, in fields or through dedicated methods
		 * 	Context Types: 
		 * 			1.	UriInfo
		 * 			2.	SecurityContext
		 * 			3.	HttpHeaders
		 * 			4.	Providers
		 * 			5.	Request
		 * 			6.	ContextReslovers
		 * 			7.	Servlet Types (HttpServletRequest, HttpSerlvetResponse, ServletContext, ServletConfig)	
		 *  
		 */

		System.out.println("Uri path:"+uriInfo.getPath());
		System.out.println("Uri abslute path:"+uriInfo.getAbsolutePath());
		System.out.println("Base Uri:"+uriInfo.getBaseUri());
		System.out.println("request uri:"+uriInfo.getRequestUri());
		
		System.out.println("media Type:"+httpHeaders.getMediaType());
		System.out.println("Length :"+httpHeaders.getLength());
		
		
		/* in this example we have hard coded for simplicity  */
		Category category = new Category();
		category.setCategoryId("001");
		category.setCategoryName("IT");
		List<Book> booksList = new ArrayList<Book>();
		Book b1 = new Book();
		b1.setAuthor("Method with UriInfo and HttpHeaders");
		b1.setBookId("book-it-1");
		b1.setBookISBNnumber("123456789");
		b1.setBookName("book name 1");
		booksList.add(b1);
		category.setBooks(booksList);
		
		return category;
	}
}
